<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="formVisible" append-to-body width="800px">
      <el-form ref="myForm" :model="form" label-width="76px">
        <el-form-item label="标题" required prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="描述" required prop="description">
          <el-input v-model="form.description" placeholder="请输入描述" />
        </el-form-item>
        <el-form-item label="排序" required prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="内容" required prop="content">
          <tinymce v-model="form.content" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSubmit">提交信息</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>

    <el-form :inline="true" size="small">
      <el-form-item label="搜索:">
        <el-input v-model="searchParams.search" placeholder="请输入搜索内容" clearable />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="fetchData">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="success" icon="el-icon-circle-plus" @click="handleInsert">添加公告</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="ID">
        <template slot-scope="scope">{{ scope.row.id }}</template>
      </el-table-column>
      <el-table-column align="center" label="标题">
        <template slot-scope="scope">{{ scope.row.title }}</template>
      </el-table-column>
      <el-table-column align="center" label="描述">
        <template slot-scope="scope">{{ scope.row.description }}</template>
      </el-table-column>
      <el-table-column align="center" label="排序">
        <template slot-scope="scope">{{ scope.row.sort }}</template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="mini" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
          <el-button
            type="text"
            size="mini"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div :style="{ textAlign: 'center', padding: '10px' }">
      <el-pagination
        background
        :current-page.sync="searchParams.pageNum"
        :page-sizes="[10, 20, 30, 40,50]"
        :page-size.sync="searchParams.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="searchParams.total"
        @size-change="fetchData"
        @current-change="fetchData"
      />
    </div>
  </div>
</template>

<script>
import { getInfoList, delInfo, saveInfo } from '@/api/system/info'

export default {
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  components: {
    tinymce: () => import('@/components/Tinymce')
  },
  data() {
    return {
      list: null,
      listLoading: true,
      formVisible: false,
      form: {
        content: ''
      },
      searchParams: {
        total: 0,
        pageNum: 1,
        pageSize: 10,
        search: ''
      },
      title: undefined
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    handleUpdate(row) {
      this.formVisible = true
      this.title = '修改公告信息'
      this.$nextTick(() => {
        this.$refs['myForm'].resetFields()
        this.form = { ...row }
      })
    },
    handleInsert() {
      this.title = '添加公告信息'
      this.formVisible = true
      this.$nextTick(() => {
        this.$refs['myForm'].resetFields()
        this.form = { content: '' }
      })
    },
    handleSubmit() {
      this.$refs['myForm'].validate(valid => {
        if (valid) {
          saveInfo(this.form).then(() => {
            this.$message.success('保存信息成功')
            this.fetchData()
            this.formVisible = false
          })
        }
      })
    },
    handleDelete({ id }) {
      this.$confirm('确认删除此信息吗？', '警告', {
        cancelButtonText: '取消',
        confirmButtonText: '确认'
      }).then(() => {
        delInfo(id).then(() => {
          this.$message.success('删除成功')
          this.fetchData()
        })
      })
    },
    fetchData() {
      this.listLoading = true
      getInfoList(this.searchParams).then(response => {
        const { records, total } = response.data
        this.list = records
        this.searchParams.total = total
        this.listLoading = false
      })
    }
  }
}
</script>

